package ru.pb.game.network.engine.client.packets.receivable;

import ru.pb.game.network.engine.client.packets.FromClientToGameServerPacket;
import ru.pb.game.network.engine.client.packets.sendable.opcode_3854_ACK;
import ru.pb.game.objects.model.players.Player;
import ru.pb.game.objects.model.players.enums.Team;
import ru.pb.game.objects.model.room.Frag;
import ru.pb.game.objects.model.room.FragInfos;
import ru.pb.game.objects.model.room.Room;
import ru.pb.game.objects.model.room.Room.SLOT;
import ru.pb.game.objects.storage.ObjectsStorage;
import ru.pb.global.network.utils.NetworkUtil;

/**
 * Вызывается когда player кого то убил.
 * 
 * @author Felixx
 */
public class opcode_3853_REQ extends FromClientToGameServerPacket
{
	private FragInfos _kills;

	public opcode_3853_REQ()
	{
		_kills = new FragInfos();
	}

	@Override
	public void readImpl()
	{
		readH(); // Фрост
		//_log.info("======= BEGIN KILL INFO REQ =====");

		_kills.unkCount = readC(); // WTF? killsCount - 1, если не убиваеш себя... если и себя то 0 будет если пострадал 1 бот и сам игрок.
		_kills.killsCount = readC(); // Всего померло.
		_kills.killer = readC(); // Слот убившего.
		_kills.weapon = readD(); // Орудие убийства.
		_kills.bytes13 = readB(13); // Хз что там

		//_log.info("unCount: " + _kills.unkCount);
		//_log.info("killsCount: " + _kills.killsCount);
		//_log.info("killer: " + _kills.killer);
		//_log.info("weapon: " + _kills.weapon);
		//_log.info("13 bytes:\n" + NetworkUtil.printData(_kills.bytes13));
		// ======================//

		for(int i = 1; i <= _kills.killsCount; i++)
		{
			Frag frag = new Frag();
			frag.unk_c_1 = readC(); //
			frag.unk_c_2 = readC(); //
			frag.unk_c_3 = readC(); //
			frag.unk_c_4 = readC(); //
			frag.bytes13 = readB(13);

			_kills.addFrag(i, frag);

			//_log.info("KILL NUMBER: " + i); //
			//_log.info("\tunk_c_1: " + frag.unk_c_1); //
			//_log.info("\tunk_c_2: " + frag.unk_c_2); //
			//_log.info("\tunk_c_3: " + frag.unk_c_3); //
			//_log.info("\tunk_c_4: " + frag.unk_c_4); //
			//_log.info("\t13 bytes:\n" + NetworkUtil.printData(frag.bytes13));
		}
		//_log.info("======= END KILL INFO REQ =====");
	}

	@Override
	public void runImpl()
	{
		Player p = getPlayer();
		if(p != null)
		{
			Room r = p.getRoom();
			if(r != null)
			{
				SLOT killer = r.getSlots()[_kills.killer];
				for(int i = 1; i <= _kills.killsCount; i++)
				{
					Frag f = _kills.gerFrag(i);
					SLOT loser = r.getSlot(f.getDeatSlot());
					loser.oneTimeKills = 0;
					loser.allDeaths++;

					boolean suicide = f.getDeatSlot() == _kills.killer;
					if( !suicide)
					{
						killer.allKills++;
						killer.oneTimeKills++;

						if(f.getDeatSlot() % 2 == 0)
						{
							r.addDeaths(Team.CHARACTER_TEAM_RED);
							r.addKills(Team.CHARACTER_TEAM_BLUE);
						}
						else
						{
							r.addDeaths(Team.CHARACTER_TEAM_BLUE);
							r.addKills(Team.CHARACTER_TEAM_RED);
						}
					}
				}

				Player member = null;
				for(int objId : r.getAllPlayers().toArray())
				{
					member = ObjectsStorage.getInstance().getPlayer(objId);
					member.sendPacket(new opcode_3854_ACK(member, _kills));
				}
			}
		}
	}
}